package com.travelplanning.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.travelplanning.admin.common.biz.user.UserContext;
import com.travelplanning.admin.common.convention.errorcode.BaseErrorCode;
import com.travelplanning.admin.common.enums.UserErrorCodeEnum;
import com.travelplanning.admin.common.exception.ClientException;
import com.travelplanning.admin.common.exception.ServiceException;
import com.travelplanning.admin.dao.entity.CommentDO;
import com.travelplanning.admin.dao.entity.UserDO;
import com.travelplanning.admin.dao.mapper.CommentMapeper;
import com.travelplanning.admin.dao.mapper.ContentMapeper;
import com.travelplanning.admin.dao.mapper.UserMapeper;
import com.travelplanning.admin.dto.req.CommentAddReqDTO;
import com.travelplanning.admin.dto.req.UserLoginReqDTO;
import com.travelplanning.admin.dto.req.UserRegisterReqDTO;
import com.travelplanning.admin.dto.req.UserUpdateReqDTO;
import com.travelplanning.admin.dto.resp.CommentQueryResqDTO;
import com.travelplanning.admin.dto.resp.UserLoginRespDTO;
import com.travelplanning.admin.dto.resp.UserRespDTO;
import com.travelplanning.admin.service.CommentService;
import com.travelplanning.admin.service.UserService;
import com.travelplanning.admin.util.JWTUtils;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.travelplanning.admin.common.constant.RedisCacheConstant.LOCK_USER_LOGIN_KEY;
import static com.travelplanning.admin.common.constant.RedisCacheConstant.LOCK_USER_REGISTER_KEY;
import static com.travelplanning.admin.common.enums.UserErrorCodeEnum.USER_NAME_EXIST;


/**
 * 用户接口实现层
 */
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapeper, CommentDO> implements CommentService {
    private final ContentMapeper contentMapeper;


    @Override
    public Void addComment(CommentAddReqDTO requestParam) {

        CommentDO comment= CommentDO.builder()
                .contentId(requestParam.getContentId())
                .article(requestParam.getArticle())
                .userId(UserContext.getUserId())
                .username(UserContext.getUsername())
                .build();
        int insert = baseMapper.insert(comment);
        if (insert<1){
            throw new ClientException("发表评论失败");
        }
        return null;
    }

    @Override
    public List<CommentQueryResqDTO> queryAllComments(String requestParam) {
        LambdaQueryWrapper<CommentDO> queryWrapper = Wrappers.lambdaQuery(CommentDO.class)
                .eq(CommentDO::getContentId, requestParam);
        List<CommentDO> commentList = baseMapper.selectList(queryWrapper);
        List<CommentQueryResqDTO> commentQueryResqDTOList=new ArrayList<>();
        commentList.forEach(commentDO -> {
            CommentQueryResqDTO commentQueryResqDTO = CommentQueryResqDTO.builder()
                    .article(commentDO.getArticle())
                    .createTime(commentDO.getCreateTime())
                    .username(commentDO.getUsername())
                    .build();

            commentQueryResqDTOList.add(commentQueryResqDTO);
        });
        return commentQueryResqDTOList;
    }


}
